<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="配资订单号">
                <a-input v-model="queryParam.netsignContractNo" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="配资服务商">
                <a-select v-model="queryParam.capitalId" placeholder="请选择">
                  <a-select-option :value="item.capitalId" v-for="item in capitalSelList" :key="item.capitalId">{{
                    item.capitalName
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="商户">
                  <a-input v-model="queryParam.merchantKeyword" placeholder="" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="配资状态">
                  <a-select v-model="queryParam.contractStatus" placeholder="请选择">
                    <a-select-option value="1">服务费待支付</a-select-option>
                    <a-select-option value="2">还款中</a-select-option>
                    <a-select-option value="3">已完结</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="回款状态">
                  <a-select v-model="queryParam.isUnfreeze" placeholder="请选择">
                    <a-select-option value="0">已冻结</a-select-option>
                    <a-select-option value="1">未冻结</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="签约开始时间">
                  <a-date-picker
                    v-model="queryParam.startNetsignCompleteTime"
                    style="width: 100%"
                    placeholder="签约开始时间"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="签约结束时间">
                  <a-date-picker
                    v-model="queryParam.endNetsignCompleteTime"
                    style="width: 100%"
                    placeholder="签约结束时间"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="创建开始时间">
                  <a-date-picker v-model="queryParam.startCreateTime" style="width: 100%" placeholder="创建开始时间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="创建结束时间">
                  <a-date-picker v-model="queryParam.endCreateTime" style="width: 100%" placeholder="创建结束时间" />
                </a-form-item>
              </a-col>
            </template>

            <a-col :md="advanced ? 24 : 8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd" v-if="btnsPurviewData.add.isShow">新建</a-button>
        <!-- <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /> </a-button>
        </a-dropdown> -->
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="contractNo"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
        listName="dataList"
        :scroll="{ x: 'calc(3000px + 100%)' }"
        v-if="btnsPurviewData.query.isShow"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <div slot="merchantInfo" slot-scope="text, record">
          <div>{{ record.merchantName }}</div>
          <div>
            <span>{{ record.merchantContactName }}</span
            >&nbsp;&nbsp;<span>{{ record.merchantContactMobile }}</span>
          </div>
        </div>
        <span slot="contractStatus" slot-scope="text">
          <span>{{ text | contractStatusFilter }}</span>
        </span>
        <span slot="isUnfreeze" slot-scope="text">
          <span>{{ text | isUnfreezeFilter }}</span>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="_toDtl(record)" v-if="btnsPurviewData.detail.isShow">查看详情</a>
          </template>
        </span>
      </s-table>
      <create-form
        ref="createForm"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :title="formTitle"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { BTNS_PURVIEW_MAP } from '@/store/mutation-types'
import storage from 'store'
import { STable, Ellipsis } from '@/components'
import { getMerchantContractList, addMerchantContract } from '@/api/merchantContractManage'
import { getCapitalSelList } from '@/api/common'
import CreateForm from '../modules/CreateContractForm'
const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' },
    align: 'center',
    fixed: 'left',
    width: 80
  },
  {
    title: '所属商户',
    scopedSlots: { customRender: 'merchantInfo' },
    align: 'center'
  },
  {
    title: '配资订单号',
    dataIndex: 'contractNo',
    align: 'center'
  },
  {
    title: '配资服务商名称',
    dataIndex: 'capitalName',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '配资金额（元）',
    dataIndex: 'loanAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '配资还款金额（元）',
    dataIndex: 'repaymentAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '配资已分账金额（元）',
    dataIndex: 'allocateAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '月还款金额（元）',
    dataIndex: 'monthRepaymentAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '服务费金额（元）',
    dataIndex: 'serviceFeeAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '还款日',
    dataIndex: 'repaymentDay',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '合同数',
    dataIndex: 'contractNums',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '合同金额（元）',
    dataIndex: 'contractAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '合同已还款金额（元）',
    dataIndex: 'contractRepaymentAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '合同标记支付金额（元）',
    dataIndex: 'contractMarkPayAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '待分账金额（元）',
    dataIndex: 'contractAllocateAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },

  {
    title: '签约完成时间',
    dataIndex: 'netsignCompleteTime',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '签约地址',
    dataIndex: 'netsignUrl',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '资产包状态',
    dataIndex: 'contractStatus',
    scopedSlots: { customRender: 'contractStatus' },
    align: 'center'
  },
  {
    title: '商户回款状态',
    dataIndex: 'isUnfreeze',
    scopedSlots: { customRender: 'isUnfreeze' },
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '120px',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right'
  }
]

const statusMap = {
  1: {
    status: 'processing',
    text: '启用'
  },
  2: {
    status: 'default',
    text: '冻结'
  }
}

const contractStatusMap = {
  1: {
    text: '服务费待支付'
  },
  2: {
    text: '还款中'
  },
  3: {
    text: '已完结'
  }
}

const isUnfreezeMap = {
  0: {
    text: '冻结'
  },
  1: {
    text: '解除冻结'
  }
}

export default {
  name: 'MerchantContractList',
  components: {
    STable,
    Ellipsis,
    CreateForm
  },
  data () {
    this.columns = columns
    return {
      btnsPurviewMap: storage.get(BTNS_PURVIEW_MAP),
      btnsPurviewData: {},
      // create model
      visible: false,
      freezeVisible: false,
      confirmLoading: false,
      mdl: null,
      formTitle: '新增配资',
      capitalSelList: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getMerchantContractList(requestParameters)
          .then((res) => {
            return res.result
          })
          .catch((e) => {})
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    },
    contractStatusFilter (type) {
      return contractStatusMap[type].text
    },
    isUnfreezeFilter (type) {
      return isUnfreezeMap[type].text
    }
  },
  created () {
    /**
     * 获取配资服务商选择列表
     */
    getCapitalSelList()
      .then((res) => {
        this.capitalSelList = res.dataList
      })
      .catch((e) => {})
    this.btnsPurviewData = this.btnsPurviewMap[this.$route.meta.permission[0]]
  },
  computed: {},
  methods: {
    /**
     * 查看详情
     */
    _toDtl (record) {
      this.$router.push(`/merchantContractManage/MerchantContractDtl/${record.contractNo}`)
    },
    /**
     * 切换searchBar展开/关闭
     */
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /**
     * 新增配资合同
     */
    handleAdd () {
      this.mdl = null
      this.visible = true
      const form = this.$refs.createForm.form
      form.resetFields() // 清理表单数据（可不做）
      this.formTitle = '新增配资'
    },
    /**
     *  新建/修改角色
     */
    handleOk (opts) {
      const form = this.$refs.createForm.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          this.confirmLoading = false
          addMerchantContract({ ...values, contractList: opts.contractList })
            .then((res) => {
              this.visible = false
              this.$refs.table.refresh()
              this.$message.info('新增成功')
            })
            .catch((e) => {})
        } else {
          this.confirmLoading = false
        }
      })
    },
    /**
     * 取消表单
     */
    handleCancel () {
      this.mdl = null
      this.visible = false
    }
  }
}
</script>
